package com.ztesoft.book.admin.controller.exportdemo;

import com.ztesoft.book.common.utils.Result;
import com.ztesoft.book.common.utils.export.word.ImageEntity;
import com.ztesoft.book.common.utils.export.word.ImageUtils;
import com.ztesoft.book.common.utils.export.word.PoiWordUtils;
import com.ztesoft.book.common.utils.export.word.WordUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zl
 * @ dateTime 2022/7/29 3:37 下午
 * @ description 导出demo
 */

@Slf4j
@RestController
@RequestMapping("/export/demo")
public class DemoController {

    @GetMapping("/exportWord")
    public @ResponseBody
    Result getSysDictListByTypeCode(HttpServletResponse response) {
        try {
            // 模板全的路径
            String templatePaht = "https://djyzycjtest.iwhalecloud.com/fileimages/jiangbei/saas2/publicFile/test/wordDemo.docx";
            Map<String, Object> paramMap = new HashMap<>(16);
            // 普通的占位符示例 参数数据结构 {str,str}
            paramMap.put("title", "模板导出标题");
            paramMap.put("name", "模板导出名称");
            paramMap.put("startYear", "2010");
            paramMap.put("endYear", "2020");
            paramMap.put("currentYear", LocalDate.now().getYear());
            paramMap.put("currentMonth", LocalDate.now().getMonthValue());
            paramMap.put("currentDate", LocalDate.now().getDayOfMonth());
            // 段落中的动态段示例 [str]
            List<Object> list1 = new ArrayList<>(Arrays.asList("2、列表2", "3、列表3"));
            paramMap.put("list1", list1);
            // 表格中的参数示例 参数数据结构 [[str]]
            List<List<String>> tbRow1 = new ArrayList<>();
            List<String> tbRow1_row1 = new ArrayList<>(Arrays.asList("1、模块一", "分类1"));
            List<String> tbRow1_row2 = new ArrayList<>(Arrays.asList("2、模块二", "分类2"));
            tbRow1.add(tbRow1_row1);
            tbRow1.add(tbRow1_row2);
            paramMap.put(PoiWordUtils.addRowText + "tb1", tbRow1);
            // 支持在表格中动态添加图片
            List<List<String>> tbRow2 = new ArrayList<>();
            List<String> tbRow2_row1 = new ArrayList<>(Arrays.asList("03栏目", "旅游方案", "方案1", "w3", "w3", "w3", "w3", "w3", "w3"));
            List<String> tbRow2_row2 = new ArrayList<>(Arrays.asList("04栏目", "上市方案", "方案1", "w5", "w5", "w5", "w5", "w5", "w5"));
            List<String> tbRow2_row3 = new ArrayList<>(Arrays.asList("05图片", "${image:image3}", "w5", "w5", "w5", "w5", "w5", "w5", "w5"));
            tbRow2.add(tbRow2_row1);
            tbRow2.add(tbRow2_row2);
            tbRow2.add(tbRow2_row3);
            ImageEntity imgEntity3 = new ImageEntity();
            imgEntity3.setHeight(100);
            imgEntity3.setWidth(100);
            imgEntity3.setUrl("https://djyzycjtest.iwhalecloud.com/fileimages/jiangbei/saas2/publicFile/test/111.jpg");
            imgEntity3.setTypeId(ImageUtils.ImageType.JPG);
            paramMap.put(PoiWordUtils.addRowText + "tb2", tbRow2);
            paramMap.put("image:image3", imgEntity3);

            // 图片占位符示例 ${image:imageid} 比如 ${image:image1}, ImageEntity中的值就为image:image1
            // 段落中的图片
            ImageEntity imgEntity1 = new ImageEntity();
            imgEntity1.setHeight(500);
            imgEntity1.setWidth(400);
            imgEntity1.setUrl("https://djyzycjtest.iwhalecloud.com/fileimages/jiangbei/saas2/publicFile/test/111.jpg");
            imgEntity1.setTypeId(ImageUtils.ImageType.JPG);
            paramMap.put("image:image1", imgEntity1);
            
            
            // 表格中的图片
            ImageEntity imgEntity2 = new ImageEntity();
            imgEntity2.setHeight(200);
            imgEntity2.setWidth(100);
            imgEntity2.setUrl("https://djyzycjtest.iwhalecloud.com/fileimages/jiangbei/saas2/publicFile/test/111.jpg");
            imgEntity2.setTypeId(ImageUtils.ImageType.JPG);
            paramMap.put("image:image2", imgEntity2);
            paramMap.put("tableName", "表格名称");
            paramMap.put("tableContent", "表格内容");
            paramMap.put("tableContentTwo", "表格内容2");
            
            WordUtils.process(paramMap, templatePaht, response, "导出模板.doc");
            return Result.success();
        }
        catch (Exception ex) {
            log.error(ex.getMessage());
            return Result.error("获取失败！");
        }
    }
}
